//
//  Presentation.m
//  ipadclient
//
//  Created by Stéphane Prohaszka on 27/04/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Presentation.h"

#import "Speaker.h"

@implementation Presentation

static NSString* const kAssetURL = @"http://www.parleys.com/assets/presentations/";
static NSString* const kAsset = @"http://s1.parleys.com/parleys/_definst_/2103/mp4:201011240955287223769.mp4/playList.m3u8";

@synthesize identification;
@synthesize title;
@synthesize thumbnail;
@synthesize summary;
@synthesize duration;
@synthesize totalVotes;
@synthesize totalDownloads;
@synthesize speakers;
@synthesize keywords;
@synthesize type;
@synthesize asset;


- (id) init {
    self = [super init];
    
    if (self) {
        identification = 0;
        title = nil;
        thumbnail = nil;
        speakers = [[NSMutableSet alloc] init];
        keywords = [[NSMutableSet alloc] init];
        type = nil;
        asset = nil;
    }
    
    return self;
}

- (id) initWithJson:(NSDictionary*)jsonObject {
    self = [super init];
    
    if (self) {
        identification = [[jsonObject objectForKey:@"id"] intValue];
        title = [[jsonObject objectForKey:@"title"] retain];
        //thumbnail = [[NSString stringWithFormat:@"%@%i%@", kAssetURL, identification, [jsonObject objectForKey:@"thumbnail"]] retain];
        NSLog(@"Thumbnail: %@", [jsonObject objectForKey:@"thumbnail"]);
        thumbnail = [[jsonObject objectForKey:@"thumbnail"] retain];
        summary = [[jsonObject objectForKey:@"summary"] retain];
        duration = [[jsonObject objectForKey:@"duration"] floatValue];
        totalVotes = [[jsonObject objectForKey:@"totalVotes"] intValue];
        totalDownloads = [[jsonObject objectForKey:@"totalDownloads"] intValue];
        speakers = [[NSMutableSet alloc] init];
        keywords = [[NSMutableSet alloc] init];
        type = [[jsonObject objectForKey:@"type"] retain];
        NSLog(@"Presentation type: %@", type);
        if ([type isEqualToString:@"VIDEO_AND_SLIDES"]) {
            // Retrieve mp3 number
            NSArray* componenents = [[jsonObject objectForKey:@"MP3URL"] componentsSeparatedByString:@"/"];
            componenents = [[componenents lastObject] componentsSeparatedByString:@"."];
            
            
            asset = [[jsonObject objectForKey:@"MP3URL"] substringToIndex:30];
            NSLog(@"Asset(1): %@", asset);
            asset = [asset stringByAppendingFormat:@"_definst_/%i/mp4:%@.mp4/playlist.m3u8", identification, [componenents objectAtIndex:0]];
            NSLog(@"Asset(2): %@", asset);
            asset = [asset stringByReplacingOccurrencesOfString:@"rtmp" withString:@"http"];
            NSLog(@"Asset(3): %@", asset);
            [asset retain];
        } else {
            asset = [[jsonObject objectForKey:@"MP3URL"] retain];
        }
    }
    
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    
    if (self) {
        identification = [aDecoder decodeIntForKey:@"identification"];
        title = [aDecoder decodeObjectForKey:@"title"];
        speakers = [aDecoder decodeObjectForKey:@"speakers"];
        keywords = [aDecoder decodeObjectForKey:@"keywords"];
    }
    
    return self;
}

- (void)dealloc {
    [title release];
    [speakers release];
    if (type != nil) {
        [type release];
    }
    if (asset != nil) {
        [asset release];
    }
    [super dealloc]; 
}

- (id)copyWithZone:(NSZone *)zone {
    Presentation* copy = [[[self class] allocWithZone:zone] init];
    copy.identification = self.identification;
    copy.title = [self.title copyWithZone:zone];
    copy.speakers = [self.speakers copyWithZone:zone];
    copy.keywords = [self.keywords copyWithZone:zone];
    
    return copy;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
	[aCoder encodeInt:identification forKey:@"identification"];
	[aCoder encodeObject:title forKey:@"title"];
	[aCoder encodeObject:speakers forKey:@"speakers"];
	[aCoder encodeObject:keywords forKey:@"keywords"];
}

- (void) addSpeaker:(Speaker*)speaker {
    [speakers addObject:speaker];
}

- (NSSet*) getSpeakers {
    return speakers;
}

- (void) addKeywords:(NSString*)keyword {
    [keywords addObject:keyword];
}

@end
